﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
public partial class Admin_QuanLyQuanTriVien_Themmoiquantrivien : System.Web.UI.Page
{
    private DataAccess da = new DataAccess();
    private Entities en = new Entities();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
    }
    protected bool ExistsUser(string strUserName)
    {
        bool bOutPut = false;
        SqlDataReader drd = null;
        SqlCommand comm = new SqlCommand("isExistsUser", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@username", strUserName);
        try
        {
            comm.Connection.Open();
            drd = comm.ExecuteReader();
            if (drd.Read())
                bOutPut = true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        finally
        {
            comm.Connection.Close();
            comm.Connection.Dispose();
        }
        return bOutPut;
    }
    protected void QT_AddNew_Click(object sender, EventArgs e)
    {
        string maqt = this.txtmaqt.Text;
        string strUserName = this.txtu.Text;
        string strPass = this.txtp.Text;
        if (ExistsUser(strUserName) != true)
        {
            SqlCommand comm = new SqlCommand("Insert_QT", da.SqlConn());
            comm.CommandType = CommandType.StoredProcedure;

            comm.Parameters.AddWithValue("@ID", maqt);
            comm.Parameters.AddWithValue("@UserName",strUserName);
            comm.Parameters.AddWithValue("@Pass", en.EncodePassword(strPass.Trim(),""));
            try
            {
                comm.Connection.Open();
                if (comm.ExecuteNonQuery() == -1)
                {
                    Response.Redirect("Danhsachquantri.aspx");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                comm.Connection.Close();
                comm.Connection.Dispose();
                Response.Redirect("Danhsachquantri.aspx");
            }
        }
        else
        {
            HttpContext.Current.Response.Write("<script language=\"javascript\">alert('Tên truy cập này đã tồn tại !');</script>");
        }

    }
    
}
